package com.atguigu.lease.app.controller;

import com.atguigu.lease.app.service.LeaseTermService;
import com.atguigu.lease.app.service.RoomLeaseTermService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.LeaseTerm;
import com.atguigu.lease.model.bean.RoomLeaseTerm;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Tag(name="租期模块")
@RestController
@RequestMapping("/app/term/listByRoomId")
public class TermController {
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Operation(summary = "根据房间id查询他的租期列表")
    @GetMapping
    public Result listByRoomId(@RequestParam Long id){
        //select lease_term_id from room_lease_term where is_deleted =0 and room_id = #{id}
        List<Long> leaseTermIds = roomLeaseTermService.list(Wrappers.lambdaQuery(RoomLeaseTerm.class)
                        .eq(RoomLeaseTerm::getRoomId, id)
                        .select(RoomLeaseTerm::getLeaseTermId))
                .stream()
                .map(RoomLeaseTerm::getLeaseTermId)
                .collect(Collectors.toList());
        //select from lease_term where is_deleted = 0 and id in(上面查询的结果集)
        return Result.ok(leaseTermService.list(Wrappers.lambdaQuery(LeaseTerm.class)
                .in(LeaseTerm::getId , leaseTermIds)));

    }


}
